import sys
from typing import Set, Any, Union
# -*- coding: gbk -*-
import pymysql
sys.path.extend(["F:/ML/BNC", "D:/BNC", "/www/wwwroot/BNC"])

from DataBase.DatabaseConsts import StockOneDayKLineDB, StockTagRenameTableName, CodeTagDateTableName, \
    AStockTagRelationTable

host = '127.0.0.1'
port = 3306
db = StockOneDayKLineDB
user = 'root'
password = 'h303567469'

connection = pymysql.connect(host=host,
                             port=port,
                             user=user,
                             password=password,
                             db=db,
                             charset='utf8')

curr = connection.cursor()


def main():
    right_word = {"一号文件", "数字乡村", "移动支付", "独角兽概念", "网络游戏", "氟化工", "芯片概念", "雄安新区", "稀土永磁", "MSCI预期", "染料", "创投", "玻尿酸",
                  "蚂蚁金服概念", "机器人概念", "福建自贸区", "国资驰援", "工业互联网", "金控平台", "基因测序", "智慧政务", "工业4.0", "工业大麻", "物联网", "高送转预期",
                  "量子通信", "3D玻璃", "数字孪生", "可燃冰", "白酒概念", "债转股", "数字中国", "期货概念", "钌催化剂", "超清视频", "核电", "页岩气", "集成电路概念",
                  "小米概念", "参股券商", "数字货币", "网络安全", "智能汽车", "氢能源", "人造肉", "王者荣耀", "燃料电池", "固废处理", "乡村振兴", "互联网彩票", "VPN",
                  "能源互联网", "军民融合", "金融科技", "锂电池", "手机游戏", "OLED", "煤化工", "特斯拉", "苹果概念", "PPP概念", "钨", "智能电网", "高端装备",
                  "充电桩", "电力物联网", "进口博览会", "国产软件", "猪肉", "边缘计算", "光伏概念", "一带一路", "黄金概念", "无人零售", "壳资源", "共享单车", "互联网金融",
                  "光刻胶", "卫星导航", "无人驾驶", "甲醇", "富勒烯", "饲料", "青蒿素", "轨道交通", "钛白粉", "新零售", "智能音箱", "宁德时代概念", "知识产权保护",
                  "医疗器械概念", "区块链", "云计算", "虚拟现实", "垃圾分类", "透明工厂", "车联网", "中船系", "风电", "新材料概念", "核高基", "农机", "粤港澳大湾区",
                  "特高压", "安防", "天然气", "赛马概念", "新能源汽车", "IGBT", "装配式建筑", "生物医药", "柔性屏", "国产操作系统", "上海国资改革", "云通信",
                  "文化传媒", "草甘膦", "世界杯", "5G", "二胎概念", "石墨烯", "人工智能", "大飞机", "双十一", "高校", "恒大概念", "冷链物流", "磷化工", "华为概念",
                  "橡胶", "土地流转", "富士康概念", "军工", "供应链金融", "信托概念", "钴", "高铁", "水泥", "大数据", "ETC", "无人机", "铜", "融资融券",
                  "污水处理"}

    right_map = {
        "高清视频": "超清视频",
        "手游": "手机游戏",
        "与富士康合作": "富士康概念",
        "高送转填权": "高送转预期",
        "稀土": "稀土永磁",
        "网游": "网络游戏",
        "新零售概念": "新零售",
        "轮胎": "绿色轮胎",
        "VR": "虚拟现实",
        "长三角": "长三角一体化",
        "大气治理": "尾气治理",
        "光伏": "光伏玻璃",
        "PCB板": "集成电路概念",
        "大湾区": "粤港澳大湾区",
        "地热": "地热能",
        "氢能": "氢能源",
        "家电": "家用电器",
        "钢铁": "互联网钢铁",
        "广电": "广播电视",
        "次新股": "新股与次新股",
        "CDR": "CAD",
        "互金": "互联网金融",
        "消费": "大消费",
        "高送转除权": "高送转预期",
        "金融IT": "金融科技",
        "航空": "通用航空",
        "0LED": "OLED",
        "股转": "股权转让",
        "智慧医疗": "智能医疗",
        "轨交": "轨道交通",
        "高送": "高送转预期",
        "新疆建设": "新疆建设兵团",
        "国产系统": "国产操作系统",
        "传媒": "文化传媒",
        "跟风雄安环保": "雄安环保",
        "LED": "OLED",
        "造纸": "造纸转暖",
        "高送预期": "高送转预期",
        "央企混改": "央企国资改革",
        "徐翔概念": "徐翔概念股",
        "氢燃料电池": "燃料电池",
        "雄安": "雄安新区",
        "芯片新材料": "芯片材料",
        "金刚石": "金刚石（线）",
        "锂电": "锂电池",
        "创业板壳资源": "壳资源",
        "中科院": "中科院系",
        "网安": "网络安全",
        "高送转": "高送转预期",
        "富时罗素概念": "富时罗素概念股",
        "保险": "参股保险",
        "中朝自贸": "中朝贸易区",
        "增持": "兜底增持",
        "三元锂电池": "锂电池",
        "稀土涨价": "稀土永磁",
        "油改": "油改气",
        "知识产权": "知识产权保护",
        "建材": "工程建材",
        "粵港澳大湾区": "粤港澳大湾区",
        "软件": "国产软件",
        "上海自贸": "上海自贸区",
        "环保": "节能环保",
        "氨纶涨价": "氨纶",
        "甲醇汽车": "甲醇",
        "工业4.": "工业4.0",
        "中成药": "中医药",
        "水处理": "污水处理",
        "新三板改革": "参股新三板",
        "磷涨价": "磷",
        "彩票": "互联网彩票",
        "创业板重组": "创业板重组松绑",
        "贸易战": "贸易战受益股",
        "人民币贬值": "人民币贬值受益",
        "超高清视频": "超清视频",
        "贸易战受益": "贸易战受益股",
        "券商": "参股券商",
        "纳米银": "纳米",
        "上海国资": "上海国资改革",
        "工业互联": "工业互联网",
        "军工股": "军工",
        "机械设备": "机械装备",
        "带一路": "一带一路",
        "无人汽车": "无人驾驶",
        "大麻": "工业大麻",
        "5g": "5G",
        "电路板": "集成电路概念",
        "北斗导航": "卫星导航",
        "郭T铭": "郭台铭概念",
        "家电消费促进": "家用电器",
        "干细胞或成人造肉制备": "干细胞",
        "密码": "密码安全管理",
        "分拆": "分拆上市意愿",
        "大湾": "粤港澳大湾区",
        "脑科学": "人脑工程",
        "有色": "有色金属",
        "成渝城市群": "成渝特区",
        "东北振兴": "振兴东北",
        "油服": "油气开采"
    }

    new_map_2_db = dict()
    for ri_word_ in right_word:
        new_map_2_db[ri_word_] = ri_word_

    for rmk_ in right_map:
        new_map_2_db[rmk_] = right_map[rmk_]

    # 插入不存在的
    tnc_sql = "truncate {};".format(StockTagRenameTableName)
    curr.execute(tnc_sql)
    connection.commit()
    insert_sql = "insert into {} ({}.from,{}.to) values (%s,%s)".format(StockTagRenameTableName,
                                                                        StockTagRenameTableName,
                                                                        StockTagRenameTableName)
    in_res = [[mk_, new_map_2_db[mk_], ] for mk_ in new_map_2_db]
    curr.executemany(insert_sql, [[mk_, new_map_2_db[mk_], ] for mk_ in new_map_2_db])
    connection.commit()

def write_append_2_db():
    # 如果X + 概念在标准库中存在 就 增加 X + 概念
    sql = "select distinct tag from {}".format(CodeTagDateTableName)
    curr.execute(sql)
    origin_tag_li = [ r[0] for r in curr.fetchall()]
    sql = "select distinct tag from {}".format(AStockTagRelationTable)
    curr.execute(sql)
    to_tag_li = [r[0] for r in curr.fetchall()]

    right_map = dict()
    # 查看是否只差一个概念的差距
    for origin_tag_ in origin_tag_li:
        for to_tag_ in to_tag_li:
            # 查看两个的差距
            if origin_tag_ + '概念' == to_tag_ or origin_tag_ == to_tag_ + '概念':
                right_map[origin_tag_] = to_tag_
    # 将map重新做成tuple(tuple())
    right_db_list = [[rk_, right_map[rk_]] for rk_ in right_map]
    insert_sql = "insert into {} ({}.from,{}.to) values (%s,%s)".format(StockTagRenameTableName,
                                                                        StockTagRenameTableName,
                                                                        StockTagRenameTableName)
    curr.executemany(insert_sql, right_db_list)
    connection.commit()

if __name__ == '__main__':
    # main()
    write_append_2_db()
